<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @description COURSE MODEL
 */
class courses_model extends Model {
    
    private $keyword = NULL;

    /**
     * @desc Constructor
     */
    function __construct() {
        $this->keyword = COURSES_FIELD_NAME . ',' . COURSES_SUBJECT_NAME . ',' .
                COURSES_COURSE_NAME . ',' . COURSES_COURSE_DESCRIPTION;

        // Call the Model constructor
        parent::__construct();
    }

    /*
     Array
(
    [provider_id] => 1
    [course_name] => Khoa hoc lap trinh PHP
    [course_field_id] => 1
    [course_subject_id] =>
    [course_description] => Khoa PHP co Ban
    [create_date] => 2012-04-08 18:30:04
    [modified_date] => 2012-04-08 18:30:04
    [start_date] => 2012-05-30
    [end_date] =>
    [duration] => 4tiet
    [range_duration] => 3-6
    [schedule] => 6PM 2-4-6 hoac 3-5-7 hoac 7-CN
    [max_num_learner] => 30
    [address] => 27/7F Trung Chanh, Hoc Mon
    [district_id] => 19
    [province_id] => 10
    [max_day_regrister_offline] => 25/05/2012
    [range_fee] => 4
    [course_fee] => 4 trieu
    [discount_fee] => 5%
    [entry_requirement] => CC PHP
    [other_info] => Thong tin khoa hoc PHp
    [teacher] => Nguyen Van Xuan
    [current_status] => 0
)

     */

    /**
     * @desc Insert Course
     * @param <array> $arrData
     * @return <bool> true else failed or NULL
     */
    public function insert($arrData) {
        $arrDefault = array(
            COURSES_PROVIDER_ID => NULL,
            COURSES_FIELD_ID => NULL,
            COURSES_FIELD_NAME => NULL,
            COURSES_SUBJECT_ID => NULL,
            COURSES_SUBJECT_NAME => NULL,
            COURSES_COURSE_NAME => NULL,
            COURSES_COURSE_DESCRIPTION => NULL,
            COURSES_CREATE_DATE => NULL,
            COURSES_MODIFIED_DATE => NULL,
            COURSES_START_DATE => NULL,
            COURSES_END_DATE => NULL,
            COURSES_DURATION => NULL,
            COURSES_RANGE_DURATION => NULL,
            COURSES_SCHEDULE => NULL,
            COURSES_MAX_NUM_LEARNER => NULL,
            COURSES_ADDRESS => NULL,
            COURSES_DISTRICT_ID => NULL,
            COURSES_PROVINCE_ID => NULL,
            COURSES_MAX_DAY_REGISTER_OFFLINE => NULL,
            COURSES_RANGE_FEE => NULL,
            COURSES_COURSE_FEE => NULL,
            COURSES_DISCOUNT_FEE => NULL,
            COURSES_ENTRY_REQUIREMENT => NULL,
            COURSES_OTHER_INFO => NULL,
            COURSES_TEACHER => NULL,
            COURSES_CURRENT_STATUS => NULL,
        );

        $arrInput = array_merge($arrDefault, $arrData);
        try {
            $this->db->insert(COURSES, $arrInput);
            $course_id = $this->db->insert_id();
        }
        catch (Exception $e) {
            print_r($e);
            
        }
        
        return $course_id;
    }

    /**
     * @desc Get Course BY ID
     * @param <type> $arrData
     * @return <type>
     */
    function getCourseById($arrData) {
        try {
            $arrCond = array(
                COURSES_COURSE_ID => $arrData['course_id']
            );
            $this->db->where($arrCond);
            $result = $this->db->get(COURSES)->row(0, 'array');
        }
        catch (Exception $e) {
            print_r($e);
        }
        return $result;
    }

    /**
     * @desc Get Course by Keyword
     * @param <type> $arrData
     * @return <array> data else null or fail
     */
    function getCourseBySearch($arrData) {
        try {
            $_cond = 'current_status IN (1,4)';

            //If has keyword
            if (!empty($arrData['keyword'])) {
                $_cond .= ' AND MATCH (' . COURSES_FULLTEXT_SEARCH . ') AGAINST (\'' . $arrData['keyword'] . '\')';
            }

            //If has start date
            if (!empty($arrData['start_date'])) {
                $_cond .= ' AND ' . COURSES_START_DATE . '=' . $arrData['start_date'];
            }

            //If has duration
            if (!empty($arrData['duration'])) {
                $_cond .= ' AND ' . COURSES_DURATION . '=\'' . $arrData['duration'] . '\'';
            }

            //If has range_duration
            if (!empty($arrData['range_duration'])) {
                $_cond .= ' AND ' . COURSES_RANGE_DURATION . '=\'' . $arrData['range_duration'].'\'';
            }

            //If has address
            if (!empty($arrData['address'])) {
                $_cond .= ' AND ' . COURSES_ADDRESS . '=\'' . $arrData['address'].'\'';
            }

            //If has range_fee
            if (!empty($arrData['range_fee'])) {
                $_cond .= ' AND ' . COURSES_RANGE_FEE . '=' . $arrData['range_fee'];
            }

            //If has course_fee
            if (!empty($arrData['course_fee'])) {
                $_cond .= ' AND ' . COURSES_COURSE_FEE . '=' . $arrData['course_fee'];
            }

            $_cond .= ' ORDER BY ' . COURSES_CREATE_DATE . 'DESC limit ' . $arrData['offset'] .
                        ',' . $arrData['limit'];

            $this->db->where($_cond);
            $result = $this->db->get(COURSES)->row();
        }
        catch (Exception $e) {
            print_r($e);
        }
        return $result;
    }

    
}

?>